package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DAOConnection {
	private static Connection connection = null;

	public static Connection getConnection() {
		// Registro do Driver do SqlServer para ser reconhecido pela classe
		// DriveManager
		String classWithNamespace = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		try {
			Class.forName(classWithNamespace);
		} catch (ClassNotFoundException cnfe) {
			System.out.println("Error loading driver: " + cnfe);
		}
		// Dados da conexão
		String url = "jdbc:sqlserver://";
		String serverName = "localhost";
		String portNumber = "1433";
		String databaseName = "Filmania";
		String username = "sa";
		String password = "senha";
		String selectMethod = "cursor";
		String connectionUrl = url + serverName + ":" + portNumber
				+ ";databaseName=" + databaseName + ";selectMethod="
				+ selectMethod + ";";
		try {
			// Objeto de conexão
			connection = DriverManager.getConnection(connectionUrl, username,
					password);
		} catch (SQLException e) {
			System.out.println("Erro abrindo conexão: " + e);
		}

		return connection;
	}

	public static void closeConnection() {
		try {
			// Fecha conexão
			if (connection != null)
				connection.close();
		} catch (SQLException e) {
			System.out.println("Erro fechando conexão: " + e);
		}
	}
}
